<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/26 0026
 * Time: 17:31
 */
namespace app\manager\model;


use app\common\helper\Obj2Array;
use app\common\model\Model;
use think\Session;

class ManagerModel extends Model {

    public function login($data) {

        if ($user = $this->getUser($data)) {
            $password = $user['password'];
            if ($this->password($data['password']) === $password) {
                // session
                $this->setLoginSession($user['id'], $user['username']);

                return true;
            } else {
                return false;
            }
        }
    }

    public function password($password) {
        return md5($password);
    }

    public function getUser($data) {
        $userM = $this->where(['username' => $data['username']])->find();
        if (!empty($userM)) {
            return $user = $userM->getData();
        }
        return false;
    }

    private function setLoginSession($uid, $username) {
        $sessionM = new Session();
        $sessionM->set('user', ['uid' => $uid, 'username' => $username, 'is_login' => true]);
    }

    public function logout() {
        $sessionM = new Session();
        $sessionM->clear();
    }
}